//
// Created by wujehy on 2020/8/23.
//

#include <chrono>
#include "Common.h"
//#include "boost/uuid/uuid.hpp"
//#include "boost/uuid/uuid_io.hpp"
//#include "boost/uuid/uuid_generators.hpp"
//#include <boost/algorithm/hex.hpp>
//#include <boost/uuid/detail/md5.hpp>


int64_t Common::jenkins_hash(const char *key, size_t len)
{
    uint64_t hash, i;
    for (hash = i = 0; i < len; ++i)
    {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

//std::string Common::get_uuid()
//{
//    boost::uuids::uuid a_uuid = boost::uuids::random_generator()(); // 这里是两个() ，因为这里是调用的 () 的运算符重载
//    std::string tmp_uuid = boost::uuids::to_string( a_uuid );
//    return tmp_uuid;
//}
int64_t Common::getTimeMicro()
{
    std::chrono::time_point<std::chrono::system_clock> p = std::chrono::system_clock::now();
    return std::chrono::duration_cast<std::chrono::microseconds>(p.time_since_epoch()).count();
}

int64_t Common::getTimeMilli()
{
    return getTimeMicro() / 1000;
}

std::string Common::Base64Encode(const unsigned char *Data, int DataByte)
{
    //编码表
    const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    //返回值
    std::string strEncode;
    unsigned char Tmp[4] = {0};
    int LineLength = 0;
    for (int i = 0; i < (int) (DataByte / 3); i++) {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        Tmp[3] = *Data++;
        strEncode += EncodeTable[Tmp[1] >> 2];
        strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
        strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
        strEncode += EncodeTable[Tmp[3] & 0x3F];
        if (LineLength += 4, LineLength == 76) {
            strEncode += "\r\n";
            LineLength = 0;
        }
    }
    //对剩余数据进行编码
    int Mod = DataByte % 3;
    if (Mod == 1) {
        Tmp[1] = *Data++;
        strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)];
        strEncode += "==";
    } else if (Mod == 2) {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
        strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)];
        strEncode += "=";
    }

    return strEncode;
}

std::string Common::Base64Decode(const char *Data, int DataByte, int &OutByte)
{
    //解码表
    const char DecodeTable[] =
            {
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    62, // '+'
                    0, 0, 0,
                    63, // '/'
                    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
                    0, 0, 0, 0, 0, 0, 0,
                    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
                    0, 0, 0, 0, 0, 0,
                    26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
                    39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
            };
    //返回值
    std::string strDecode;
    int nValue;
    int i = 0;
    while (i < DataByte) {
        if (*Data != '\r' && *Data != '\n') {
            nValue = DecodeTable[*Data++] << 18;
            nValue += DecodeTable[*Data++] << 12;
            strDecode += (nValue & 0x00FF0000) >> 16;
            OutByte++;
            if (*Data != '=') {
                nValue += DecodeTable[*Data++] << 6;
                strDecode += (nValue & 0x0000FF00) >> 8;
                OutByte++;
                if (*Data != '=') {
                    nValue += DecodeTable[*Data++];
                    strDecode += nValue & 0x000000FF;
                    OutByte++;
                }
            }
            i += 4;
        } else// 回车换行,跳过
        {
            Data++;
            i++;
        }
    }
    return strDecode;
}

//bool Common::GetMd5(std::string &str_md5, const char *const buffer, size_t buffer_size)
//{
//    if (buffer == nullptr)
//    {
//        return false;
//    }
//    boost::uuids::detail::md5 boost_md5;
//    boost_md5.process_bytes(buffer, buffer_size);
//    boost::uuids::detail::md5::digest_type digest;
//    boost_md5.get_digest(digest);
//    const auto char_digest = reinterpret_cast<const char*>(&digest);
//    str_md5.clear();
//    boost::algorithm::hex(char_digest,char_digest+sizeof(boost::uuids::detail::md5::digest_type), std::back_inserter(str_md5));
//    return true;
//}


bool Common::GetPathFileNameAndFilePath(const std::string &allPath, std::string &filePath, std::string &filename, bool isUnix )
{
    std::string lastkeywork;
    if (isUnix)
    {
        lastkeywork = "/";
    } else
    {
        lastkeywork = "\\";
    }
    int pos = allPath.find_last_of(lastkeywork);
    std::string s(allPath.substr(pos + 1));
    filename = s;
    filePath = std::string(allPath.substr(0, pos));
    if (filename.empty())
    {
        return false;
    }
    return true;
}


const std::string HexCode = "0123456789ABCDEF";

std::string  Common::StringToHex(const std::string &data)
{
    std::string result;
    uint8_t currTemp = 0;
    for (int i = 0; i < data.size(); ++i)
    {
        currTemp = data[i] & 0xff;
        result.push_back( HexCode[currTemp >> 4] );
        result.push_back( HexCode[currTemp & 0x0f] );
    }
    return result;
}

std::string  Common::HexToString(const std::string &data)
{
    std::string result;
    for (int i = 0; i < data.length(); i += 2)
    {
        std::string byte = data.substr( i, 2 );
        char chr = (char) strtol( byte.c_str(), NULL, 16 );
        result.push_back( chr );
    }
    return result;
}

